import { permissionCreateSchema } from "~/validators/permission";
export default defineEventHandler(async (event) => {
  const result = await runValidate(permissionCreateSchema, event);
  const { code, name } = result.data;
  // findOne 是 Mongoose 的方法，用于查找符合条件的第一条数据。
  const oldData = await PermissionSchema.findOne({
    $or: [{ code: code }, { name: name }],
  }).lean();
  if (oldData) {
    throw createError({
      statusCode: 409,
      statusMessage: "数据已存在，请修改后重试",
    });
  }
  const newData = await PermissionSchema.create(result.data);
  return newData;
});
